<?php 
class Tourismgoodscategoryattribute extends CS_Controller
{
    //类别
    public function _init()
    {
        $this->load->model('tourism_goods_category_model', 'tourism_goods_category');
        $this->load->model('tourism_goods_category_attribute_model', 'tourism_goods_category_attribute');
        $this->load->model('tourism_goods_category_spec_model', 'tourism_goods_category_spec');
    }
    
     /**
     *类别属性数据列表
     * @auther laona
     * test time 2015.5.7
     **/
    public function grid($category_id)
    {
        $data['category_id'] = $category_id;
        $data['result'] = $this->tourism_goods_category_attribute->getAttributeByCategoryId($category_id);
        $this->load->view('tourismgoodscategoryattribute/grid', $data);
    }
    
    /**
    * 类别属性添加
    * @auther laona
    * test time 2015.5.7
    **/
    public function add($category_id)
    {
        $data['category_id'] = $category_id;
        $data['cate'] = $this->tourism_goods_category->getCategoryByLastId($category_id);
        $this->load->view('tourismgoodscategoryattribute/add', $data);
    }
    
    /**
    *类别属性添加处理
    * @auther laona
    * test time 2015.5.7
    * */
    public function addPost()
    {
        $postData = $this->input->post();
        $alreadyAdd = $this->tourism_goods_category_attribute->total($this->input->post());
        if ($alreadyAdd > 0) {
            $this->jsonMessage('该类别属性名称已存在');
        }
        $this->db->trans_start();
        $attrId = $this->tourism_goods_category_attribute->insertCategoryAttribute($postData);
        $postData['attr_id'] = $attrId;
        $result = $this->tourism_goods_category_spec->insertCategorySpec($postData);
        $this->db->trans_complete();
        if ($this->db->trans_status() === TRUE) {
            $this->jsonMessage('',base_url('tourismgoodscategoryattribute/grid/'.$this->input->post('category_id')));
        } 
        $this->jsonMessage('添加失败,该类别已存在该属性名称');
    }
    
    /**
     * 类别属性规格修改
     * @param unknown $attr_id
     */
    public function edit($attr_id)
    {
        $data['attr_id'] = $attr_id;
        $data['cate'] = $this->getCate($attr_id);
        $data['attribute'] = $this->tourism_goods_category_attribute->getAttributeInfoByAttrId($attr_id);
        $data['spec'] = $this->tourism_goods_category_spec->getSpecByAttId($attr_id);
    
        if (!$this->validateEmpty($data)) {
            $this->error('tourismgoodscategoryattribute/grid', $attr_id, '参数有误');
        }
        $this->load->view('tourismgoodscategoryattribute/edit', $data);
    }
    
    /**
     * 根据attr_id查询类别全称
    * */
    public function getCate($attr_id)
    {
        $result = $this->tourism_goods_category_attribute->getCategoryIdByAttrId($attr_id);
        $cid = $result->category_id ? $result->category_id : 0;
        if ($cid == 0) {
            return false;
        }
        return $this->tourism_goods_category->getCategoryByLastId($cid);
    }
    
    /**
     * 类别属性规格修改处理
    * @auther laona
    * test time 2015.5.7
    * */
    public function editPost()
    {
        $postData = $this->input->post();
        $this->db->trans_start();
        $update = $this->tourism_goods_category_attribute->updateCategoryAttribute($postData);
        if(!empty($postData['attribute_value'])){
            $result = $this->tourism_goods_category_spec->insertCategorySpec($postData);
        }
        $this->db->trans_complete();
        if ($this->db->trans_status() === TRUE) {
            $this->jsonMessage('',base_url('tourismgoodscategoryattribute/grid/'.$this->input->post('category_id')));
        }
        $this->jsonMessage('修改失败');
    }
    
    /**
     * 类别属性规格删除
    * @auther laona
    * test time 2015.5.7
    * */
    public function delete($attr_id)
    {
        $category_id = $this->input->get('category_id');
        $this->db->trans_start();
        $m = $this->tourism_goods_category_attribute->deleteAttribute($attr_id);
        $n = $this->tourism_goods_category_spec->deleteSpec($attr_id);
        $this->db->trans_complete();
        
        if ($m && $n) {
            $this->success('tourismgoodscategoryattribute/grid', $category_id, '删除成功');
        } else {
            $this->error(tourismgoodscategoryattribute/grid, $category_id, '删除失败');
        }
    }
    
    /**
     * 验证数据是否完全
    * */
    public function validateEmpty($data)
    {
        if (!is_array($data)) {
            return false;
        }
        foreach ($data as $item) {
            if (empty($item)) {
                return false;
            }
        }
        return true;
    }
}